﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Enumeration;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Networking.Sockets;
using System.Collections.ObjectModel;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using System.Threading.Tasks;
using System.Threading;
using System.Text;
using Windows.Devices.Bluetooth;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace SoftKeypadUWPApp
{
    /// <summary>
    /// The Main Page for the app
    /// </summary>
    public sealed partial class MainPage : Page
    {
        string strTitle = "Simulated Phone Keypad to test Keypad Class";

        KeypadUWPLib.Keypad Keypad = null;

        public MainPage()
        {
            this.InitializeComponent();
            Title.Text = strTitle;
            SetupKPadEventHandlers();
#if IS_PC
            SetupSoftKeys();
#endif
        }


        public void SetupKPadEventHandlers()
        {
            //The keypad instance
            Keypad = new KeypadUWPLib.Keypad();
            //Specific event handlers
            Keypad.KeyDown += Keypad_KeyDown;
            Keypad.KeyUp += Keypad_KeyUp;
            Keypad.KeyHolding += Keypad_KeyHolding;
            //A common handler
            //Keypad.KeyDown += Keypad_Keys;
            //Keypad.KeyUp += Keypad_Keys;
            //Keypad.KeyHolding += Keypad_Keys;
        }

        private void WriteOutput(string msg)
        {
            Output.Text = msg + "\r\n" + Output.Text;
        }

        /// <summary>
        /// A common handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Keypad_Keys(object sender, KeypadUWPLib.KeypadEventArgs e)
        {
            //WriteOutput("Keys " + e.Key + " " + e.Action.ToString());
        }


        /// <summary>
        /// KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Keypad_KeyDown(object sender, KeypadUWPLib.KeypadEventArgs e)
        {
            WriteOutput("Down " + e.Key + " " + e.Action.ToString());
        }

        /// <summary>
        /// KeyUp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Keypad_KeyUp(object sender, KeypadUWPLib.KeypadEventArgs e)
        {
            WriteOutput("Up   " + e.Key + " " + e.Action.ToString());
        }

        /// <summary>
        /// KeyHolding
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Keypad_KeyHolding(object sender, KeypadUWPLib.KeypadEventArgs e)
        {
            WriteOutput("Holding   " + e.Key + " " + e.Action.ToString());
        }


        #region Softkeys
#if IS_PC
        /// <summary>
        /// Need IS_PC defined. Have it not defined for a touch screen
        /// Got to kludge this keypad a bit as XAML doesn't process mouse clicks on command buttons for
        /// keyress down up and hold.
        /// Note have to click on each key then type (any key) on the PCs keyboard.
        /// Can hold that keyboard key for Holding event (one of only as per Keypad class).
        /// </summary>
        enum Keystate { isup,isdown,isholding }

        private void SetupSoftKeys()
        {
            isdown = new Dictionary<char, Keystate>();
            isdown.Add('#', Keystate.isup);
            isdown.Add('*', Keystate.isup);
            isdown.Add('0', Keystate.isup);
            isdown.Add('1', Keystate.isup);
            isdown.Add('2', Keystate.isup);
            isdown.Add('3', Keystate.isup);
            isdown.Add('4', Keystate.isup);
            isdown.Add('5', Keystate.isup);
            isdown.Add('6', Keystate.isup);
            isdown.Add('7', Keystate.isup);
            isdown.Add('8', Keystate.isup);
            isdown.Add('9', Keystate.isup);
            isdown.Add('C', Keystate.isup);
            isdown.Add('T', Keystate.isup);
        }

        Dictionary<char, Keystate> isdown = new Dictionary<char, Keystate>();
        private void Button_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            Button key = (Button)sender;
            string face = (string)key.Tag;

            if (isdown[face[0]] == Keystate.isup)
            {
                isdown[face[0]] = Keystate.isdown;
                Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('+', face[0]));
            }
            else if (isdown[face[0]] == Keystate.isdown)
            {
                Button_Holding(sender, null);
            }
            //Else ignore
        }

        private void Button_KeyUp(object sender, KeyRoutedEventArgs e)
        {
            Button key = (Button)sender;
            string face = (string)key.Tag;
            isdown[face[0]] = Keystate.isup;
            Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('-', face[0]));
        }

        private void Button_Holding(object sender, HoldingRoutedEventArgs e)
        {
            Button key = (Button)sender;
            string face = (string)key.Tag;
            isdown[face[0]] = Keystate.isholding;
                Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('@', face[0]));
        }

        private void Button_Tapped(object sender, TappedRoutedEventArgs e)
        {
            //Dummy just for comiler
        }
#else
        // Use with touch screen, eg Phone
        // Get Tap event = Keydown
        //     Holding Start event = Holding
        //     Holding Complete event = Keyup
        // Note: Don't get Keyup with Tap
        private void Button_Holding(object sender, HoldingRoutedEventArgs e)
        {
            Button key = (Button)sender;
            string face = (string)key.Tag;
            if (e.HoldingState == Windows.UI.Input.HoldingState.Started)
                Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('@', face[0]));
            else
                Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('-', face[0]));

        }



        private void Button_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Button key = (Button)sender;
            string face = (string)key.Tag;
            Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('+', face[0]));
        }

        private void Button_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            //Dummy just for comiler
        }

        private void Button_KeyUp(object sender, KeyRoutedEventArgs e)
        {
            //Dummy just for comiler
        }


#endif
        #endregion

    }
}
